import time, datetime


def testtime():
    t = time.time()

    print(t)  # 原始时间数据
    print(int(t))  # 秒级时间戳
    print(str(round(t * 1000)))  # 毫秒级时间戳


def encode_b77(n):
    table = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+)(*&^%$#@!~'
    result = []
    temp = int(n)
    if 0 == temp:
        result.append('0')
    else:
        while 0 < temp:
            result.append(table[temp % 77])
            temp = int(temp / 77)
    return ''.join([x for x in reversed(result)])


def decode_b77(str):
    table = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5,
             "6": 6, "7": 7, "8": 8, "9": 9,
             "a": 10, "b": 11, "c": 12, "d": 13, "e": 14, "f": 15, "g": 16,
             "h": 17, "i": 18, "j": 19, "k": 20, "l": 21, "m": 22, "n": 23,
             "o": 24, "p": 25, "q": 26, "r": 27, "s": 28, "t": 29, "u": 30,
             "v": 31, "w": 32, "x": 33, "y": 34, "z": 35,
             "A": 36, "B": 37, "C": 38, "D": 39, "E": 40, "F": 41, "G": 42,
             "H": 43, "I": 44, "J": 45, "K": 46, "L": 47, "M": 48, "N": 49,
             "O": 50, "P": 51, "Q": 52, "R": 53, "S": 54, "T": 55, "U": 56,
             "V": 57, "W": 58, "X": 59, "Y": 60, "Z": 61,
             "-": 62, "_": 63, '=': 64, '+': 65, ')': 66, '(': 67, '*': 68,
             '&': 69, '^': 70, '%': 71, '$': 72, '#': 73, '@': 74, '!': 75,
             '~': 76}
    result = 0
    for i in range(len(str)):
        result *= 77
        result += table[str[i]]
    return result


'''
description: 以当前时间生成编码,精确到毫秒
return {str} 缩短后的编码
'''


def now_time_conversion_code():
    timeStamp = int(time.time() * 1000)
    result = encode_b77(timeStamp)
    return result


'''
description: 以编码转换为时间,精确到毫秒，%Y-%m-%d %H:%M:%S.%f
param {str}：编码字符串 
return {str} ：时间日期
'''


def code_conversion_time(str):
    timeStamp = decode_b77(str)
    d = datetime.datetime.fromtimestamp(timeStamp / 1000)
    # 精确到毫秒
    str1 = d.strftime("%Y-%m-%d %H:%M:%S.%f")
    return str1
